﻿using CinemaMaestro.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class DirectorEndPoints
    {
        private const string DirectorsUrl = "api/directors";
        private HttpClient client;

        public DirectorEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<List<DirectorViewModel>> GetAllDirectorsAsync()
        {
            List<DirectorViewModel> directors = new List<DirectorViewModel>();
            HttpResponseMessage response = await client.GetAsync(DirectorsUrl);
            if (response.IsSuccessStatusCode)
            {
                directors = await response.Content.ReadAsAsync<List<DirectorViewModel>>();
            }
            return directors;
        }

        public async Task<DirectorViewModel> GetDirectorAsync(int id)
        {
            DirectorViewModel director = null;
            HttpResponseMessage response = await client.GetAsync(DirectorsUrl + "?id=" + id);
            if (response.IsSuccessStatusCode)
            {
                director = await response.Content.ReadAsAsync<DirectorViewModel>();
            }
            return director;
        }

        public async Task CreateDirectorAsync(DirectorViewModel director)
        {
            HttpResponseMessage response = await client.PostAsJsonAsync(DirectorsUrl, director);
            response.EnsureSuccessStatusCode();
        }

        public async Task UpdateDirectorAsync(DirectorViewModel director)
        {
            HttpResponseMessage response = await client.PutAsJsonAsync(DirectorsUrl, director);
            response.EnsureSuccessStatusCode();
        }

        public async Task DeleteDirectorAsync(int id)
        {
            HttpResponseMessage response = await client.DeleteAsync(DirectorsUrl + "?id=" + id);
            response.EnsureSuccessStatusCode();
        }
    }
}
